package com.walterlife.javacore;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;

/**
 * ThreadPoolTest
 *
 * @author zhuyuwei
 * @date 2017/5/16
 */
public class ThreadPoolTest {
    private static ExecutorService executor = Executors.newFixedThreadPool(10);

    class Task implements Callable {
        private int[] array;

        public Task(int[] array) {
            this.array = array;
        }

        @Override
        public Object call() throws Exception {
            return IntStream.of(array).sum();
        }
    }

    public int sum() throws ExecutionException, InterruptedException {
        int result = 0;
        int[] arr = { 1, 2, 3, 4 };
        for (int i = 0; i < 100; i++) {
            result += (Integer) executor.submit(new Task(arr)).get();
        }
        executor.shutdown();
        return result;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println(new ThreadPoolTest().sum());
    }
}